from PyQt5.QtWidgets import QStyledItemDelegate, QApplication, QMainWindow, QTreeWidget, QTreeWidgetItem
from PyQt5.QtGui import QPainter, QIcon
from PyQt5.QtCore import Qt, QModelIndex


class RightIconDelegate(QStyledItemDelegate):
    def paint(self, painter, option, index):
        # 先调用父类的 paint 方法绘制默认内容（文字等）
        super().paint(painter, option, index)

        # 获取图标
        icon = index.data(Qt.DecorationRole)
        if icon is not None and not icon.isNull():
            # 计算图标绘制的位置（右侧）
            rect = option.rect
            icon_size = icon.actualSize(option.decorationSize)
            icon_rect = rect.adjusted(rect.width() - icon_size.width(),
                                      (rect.height() - icon_size.height()) // 2,
                                      0, 0)
            # 绘制图标
            icon.paint(painter, icon_rect)